package cn.duhengkui.national.valuelist.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

import cn.duhengkui.national.valuelist.context.EntityLoader;
import cn.duhengkui.national.valuelist.masterslavedb.TransactionManager;
import cn.duhengkui.national.valuelist.masterslavedb.ValuelistDataSource;

@Configuration
@ConditionalOnProperty(prefix = "valuelist", value = "enabled")
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
@ImportResource(locations={"classpath*:national-valuelist-config.xml"})
@EnableConfigurationProperties(ValuelistProperties.class)

public class AutoConfiguration {
	
	@Autowired
	private ValuelistProperties valuelistProperties;
	
	@Bean("dataSource")
	@ConditionalOnProperty(name = "valuelist.masterslavedb",havingValue="true")
    ValuelistDataSource getValuelistDataSource(){
    	return new ValuelistDataSource();
    }
	
	@Bean("transactionManager")
	@ConditionalOnProperty(name = "valuelist.masterslavedb",havingValue="true")
	TransactionManager getTransactionManager(){
		TransactionManager manager = new TransactionManager();
		manager.setDataSource(getValuelistDataSource());
		return manager;
    }
	
	@Bean
	EntityLoader getEntityLoader(){
		EntityLoader loader =  new EntityLoader();
		loader.setPackages(valuelistProperties.getEntitypackage());
		return loader;
	}
}
